package com.qipeijia.core.mytest;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.EnumTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/6/27.
 */
public class MyEnmeTypeHandler extends BaseTypeHandler<MyStatus> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, MyStatus e, JdbcType jdbcType) throws SQLException {
        preparedStatement.setShort(i,e.getStatus());
    }

    public MyStatus getNullableResult(ResultSet rs, String columnName) throws SQLException {
        Short s = rs.getShort(columnName);
        return getMyStatusByStatus(s);
    }

    public MyStatus getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        Short s = rs.getShort(columnIndex);
        return getMyStatusByStatus(s);
    }

    public MyStatus getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        Short s = cs.getShort(columnIndex);
        return getMyStatusByStatus(s);
    }

    private MyStatus getMyStatusByStatus(Short s){
        MyStatus result=null;
        MyStatus[] statuses=MyStatus.values();
        for (MyStatus status:statuses){
            if(status.getStatus()==s){
                result=status;
                break;
            }
        }
        return result;
    }
}
